import {
	defaultAndSuffix
} from '@/api/other.js'
import {
	config
} from '@/utils/config.js'

const upload = {
	data() {
		return {
			images: []
		}
	},
	methods: {
		// 单张上传
		handleUploadSingle() {
			const that = this
			const token = uni.getStorageSync('token');
			const header = {
				"token": token ? token : ''
			}
			uni.chooseImage({
				count: 1, //默认9
				success: (chooseImageRes) => {
					console.log(chooseImageRes, 'chooseImageRes')
					const tempFilePaths = chooseImageRes.tempFilePaths;
					uni.uploadFile({
						url: config.BASE_API + defaultAndSuffix,
						filePath: tempFilePaths[0],
						header,
						name: 'file',
						fileType: 'image',
						success: (uploadFileRes) => {
							const res = JSON.parse(uploadFileRes.data)
							console.log(res, res.code)
							if (res.code !== 200) {
								switch (res.code) {
									// 401: token失效或不存在
									case 101:
										uni.showToast({
											title: '登录信息已失效，请重新登录',
											icon: 'none',
											duration: 2000
										})
										setTimeout(() => {
											uni.removeStorageSync('token');
											uni.reLaunch({
												url: "/pages/login/index"
											})
										}, 2000)
										break
										// 其他错误，直接抛出错误提示
									default:
										uni.showToast({
											title: res.message,
											icon: 'none',
											duration: 2000
										})
								}
							} else {
								let currentImages = `${res.result}`
								console.log(currentImages)
								that.images.push(currentImages)
								that.logo = currentImages
							}
						}
					})
				},
				fail: function() {
					uni.showToast({
						title: '图片上传失败，请重试！',
						icon: 'none',
						duration: 2000
					})
				}
			})
		},

		// 删除这张图片
		handleCancel(eIndex) {
			const that = this;
			console.log('ca')
			that.images.splice(eIndex, 1)
			// uni.showModal({
			//   title: '提示!',
			//   content: '你确定要删除这张图片',
			//   success: function (res) {
			// 	if (res.confirm) {
			// 		that.images.splice(eIndex, 1)
			// 	} else if (res.cancel) {

			// 	}
			//   }
			// })
		}
	}
}
export default upload
